----------------------------------------------------------------------------------------------------------------------------------
--[[

    

]]--
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_item_spell = Class(function(self, inst)
    self.inst = inst
    

    self._owner = net_entity(inst.GUID,"loramia_com_item_spell.owner","loramia_com_item_spell.owner")
    self.owner = nil

    self.indicator_type = "circle"  -- "circle" or "arrow"
    self.fx = nil

    if not TheNet:IsDedicated() then
        local function start_mouse_event_listener_and_indicator()
            self:Client_Create_Mouse_Event_Listener()
            if self.indicator_type == "circle" then
                self:Client_Create_Dotted_Circle()
            elseif self.indicator_type == "arrow" then
                self:Client_Create_Dotted_Arrow()
            end
            self.inst:PushEvent("indicator_acitved",self.fx and self.fx:IsValid() and self.fx)
        end
        inst:ListenForEvent("loramia_com_item_spell.owner",function(inst)
            local temp_owner = self._owner:value()
            if temp_owner and temp_owner ~= inst and temp_owner == ThePlayer and TheInput then
                self.owner = temp_owner
                start_mouse_event_listener_and_indicator()
            end
        end)
        inst:ListenForEvent("owner_rpc_set_by_userid",function(inst,userid)
            if userid and userid == ThePlayer.userid and self.owner == nil then
                self.owner = ThePlayer
                start_mouse_event_listener_and_indicator()
            end
        end)
    end


end)
------------------------------------------------------------------------------------------------------------------------------
---
    function loramia_com_item_spell:SetOwner(owner)
        if TheWorld.ismastersim then
            self._owner:set(owner)
        end
        self.owner = owner
    end
------------------------------------------------------------------------------------------------------------------------------
--- 添加鼠标监听器。
    function loramia_com_item_spell:Client_Create_Mouse_Event_Listener()
        if self.special_mouse_control ~= nil then
            return
        end
        self.special_mouse_control = TheInput:AddMouseButtonHandler(function(button, down, x, y)
            if down then
                -- print("mouse click loramia_com_item_spell",button)
                if button == MOUSEBUTTON_LEFT then
                    -- ThePlayer.components.playercontroller:Enable(false)
                    self:Active_Mouse_Left_Fn()
                    ThePlayer.components.playercontroller:Enable(true)
                elseif button == MOUSEBUTTON_RIGHT then
                    self:Active_Mouse_Right_Fn()
                end
            end
        end)
        self.inst:ListenForEvent("onremove",function()
            self.special_mouse_control:Remove()
            self.special_mouse_control = nil
            -- ThePlayer.components.playercontroller:Enable(true)  --- 恢复鼠标点击移动
        end)
        -- ThePlayer.components.playercontroller:Enable(false) --- 避免鼠标点击移动
    end
------------------------------------------------------------------------------------------------------------------------------
--- 圆形指示器
    function loramia_com_item_spell:Set_Dotted_Circle_Radius(radius)
        self.radius = radius
    end
    function loramia_com_item_spell:Client_Create_Dotted_Circle(range)
        if self.fx and self.fx:IsValid() then
            return
        end
        range = range or self.radius or 8
        self.fx = SpawnPrefab("loramia_sfx_dotted_circle_client")
        self.fx:PushEvent("Set",{
            range = range
        })
        -- self.inst:DoPeriodicTask(FRAMES,function()
        --     local pt = TheInput:GetWorldPosition()
        --     if pt and self.inst:IsValid() then
        --         self.fx.Transform:SetPosition(pt.x,0,pt.z)
        --     else
        --         self.fx:Remove()
        --     end
        -- end)
        TheInput:Loramia_Add_Update_Modify_Fn(self.inst,function()
            local pt = TheInput:GetWorldPosition()
            if pt and self.inst:IsValid() then
                self.fx.Transform:SetPosition(pt.x,0,pt.z)
            else
                self.fx:Remove()
            end
        end)
        self.inst:ListenForEvent("onremove",function()
            self.fx:Remove()
        end)        
    end
------------------------------------------------------------------------------------------------------------------------------
--- 箭头指示器
    function loramia_com_item_spell:Client_Create_Dotted_Arrow()
        if self.fx and self.fx:IsValid() then
            return
        end
        local fx = SpawnPrefab("loramia_sfx_dotted_arrow_client")
        self.fx = fx
        fx:PushEvent("Set",{})
        local function arrow_update_fn()
            if ThePlayer and TheInput and fx.SetRotation then
                local x,y,z = ThePlayer.Transform:GetWorldPosition()
                local mouse_pt = TheInput:GetWorldPosition()
                fx.Transform:SetPosition(x,y,z)
                local angle = -math.deg(math.atan2(mouse_pt.z - z,mouse_pt.x - x))
                fx:SetRotation(angle)
            end
        end
        TheInput:Loramia_Add_Update_Modify_Fn(self.inst,arrow_update_fn)
        self.inst:ListenForEvent("onremove",function()
            self.fx:Remove()
        end)
    end
------------------------------------------------------------------------------------------------------------------------------
--- 鼠标左右键盘函数
    function loramia_com_item_spell:Set_Mouse_Left_Click_Fn(fn)
        self._mouse_left_click_fn = fn
    end
    function loramia_com_item_spell:Active_Mouse_Left_Fn()
        if self._mouse_left_click_fn then

            self._mouse_left_click_fn(self.inst,self.owner,TheInput:GetWorldPosition(),TheInput:GetWorldEntityUnderMouse())
        end        
    end
    function loramia_com_item_spell:Set_Mouse_Right_Click_Fn(fn)
        self._mouse_right_click_fn = fn        
    end
    function loramia_com_item_spell:Active_Mouse_Right_Fn()
        if self._mouse_right_click_fn then
            self._mouse_right_click_fn(self.inst,self.owner,TheInput:GetWorldPosition(),TheInput:GetWorldEntityUnderMouse())
        end
    end
------------------------------------------------------------------------------------------------------------------------------
---  RPC 管道
    function loramia_com_item_spell:GetRPC()
        if self.owner and self.owner.replica.loramia_com_rpc_event then
            return self.owner.replica.loramia_com_rpc_event
        end
        return nil
    end
    function loramia_com_item_spell:PushEvent(event_name,data)
        local owner_rpc = self:GetRPC()
        if owner_rpc then
            owner_rpc:PushEvent(event_name,data,self.inst)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
--- owner
    function loramia_com_item_spell:GetOwner()
        if self.owner and self.owner:HasTag("player") then
            return self.owner
        end
        return nil
    end
    function loramia_com_item_spell:GetCaster()
        return self:GetOwner()
    end
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_item_spell







